<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_alertrules.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["t_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./includes/functions_html.php");
$grid = new Grid();
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("staff_talertrules");

require_once ("./modules/tickets/functions_alertrulesui.php");
require_once ("./modules/tickets/functions_ticketsui.php");
$interface->assignQuickLink($_SWIFT["language"]["managealerts"], "index.php?_m=tickets&_a=managealerts", "icon_alertrules.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertalert"], "index.php?_m=tickets&_a=insertalert", "icon_insertalertrule.gif");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing Alert Rule data
*/
function _alertRuleFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	if ($record["alerttype"] == ALERT_SMS)
	{
		$record["alerttype"] =$_SWIFT["language"]["sms"];
	} else {
		$record["alerttype"] =$_SWIFT["language"]["email"];
	}

	if ($record["alertscope"] == "1")
	{
		$record["alertscope"] =$_SWIFT["language"]["public"];
	} else {
		$record["alertscope"] =$_SWIFT["language"]["private"];
	}

	$record["title"] = '<table width="100%"  border="0" cellspacing="0" cellpadding="1"><tr><td width="1"><img src="'. $_SWIFT["themepath"] .'icon_alertrules.gif" border="0" /></td><td><span class="smalltext">&nbsp;<a href="index.php?_m=tickets&_a=editalert&alertruleid='. $record["alertruleid"] .'">'. $record["title"] .'</a></span></td></tr></table>';

	$record["options"] = '<a href="index.php?_m=tickets&_a=editalert&alertruleid='. $record["alertruleid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["ardelconfirmmsg"] .'\', \'index.php?_m=tickets&_a=managealerts&do=delete&alertruleid='. $record["alertruleid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Mass Action: Delete Alert Rules
*/
function _maDeleteAlertRules()
{
	global $dbCore, $_SWIFT;

	if (_is_array($_POST["itemid"]))
	{
		deleteAlertRules($_POST["itemid"]);
	}
}


/**
* ###############################################
* MANAGE ALERTS
* ###############################################
*/
if ($eventaction == "managealerts")
{
	if ($_GET["do"] == "delete" && !empty($_GET["alertruleid"]))
	{
		$infomessage = $_SWIFT["language"]["ardelconfirm"];

		deleteAlertRules(array($_GET["alertruleid"]));
	}

	if (!empty($_GET["insertrule"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["arinsertconfirm"], htmlspecialchars($_GET["insertrule"]));
	} else if (!empty($_GET["editrule"])) {
		$infomessage = sprintf($_SWIFT["language"]["areditconfirm"], htmlspecialchars($_GET["editrule"]));
	}
	

	$options["recordsperpage"] = "10";
	$options["sortby"] = "title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteAlertRules";

	$options["idname"] = "alertruleid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'alertrules` WHERE `title` LIKE \'%$_searchstr%\' AND ((`alertscope` = \''. ALERT_PUBLIC .'\') OR (`alertscope` = \''. ALERT_PRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\')) $_sortjoin;';

	$options["callback"] = "_alertRuleFields";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["ruletitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "alerttype";
	$fields[1]["title"] = $_SWIFT["language"]["alerttype"];
	$fields[1]["width"] = "150";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "alertscope";
	$fields[2]["title"] = $_SWIFT["language"]["alertscope"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	renderTicketTree();
	$interface->staffHeader($_SWIFT["language"]["tickets"]." &gt; ".$_SWIFT["language"]["managealerts"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managealerts" title="'.$_SWIFT["language"]["managealerts"].'">'.$_SWIFT["language"]["managealerts"].'</a>', "", 2);

	$grid->start("alerts", $_SWIFT["language"]["alertrules"], 'SELECT * FROM `'. TABLE_PREFIX .'alertrules` WHERE (`alertscope` = \''. "1" .'\') OR (`alertscope` = \''. "0" .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\') $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'alertrules` AS alertrules WHERE (`alertscope` = \''. ALERT_PUBLIC .'\') OR (`alertscope` = \''. ALERT_PRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\');', 'SELECT * FROM `'. TABLE_PREFIX .'alertrules` WHERE `title` LIKE \'%$_searchstr%\' AND ((`alertscope` = \''. ALERT_PUBLIC .'\') OR (`alertscope` = \''. ALERT_PRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\')) $_sortjoin;', $fields, $options);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	$grid->display("alerts");

	$interface->staffFooter();





/**
* ###############################################
* INSERT ALERT RULE
* ###############################################
*/
} else if ($eventaction == "insertalert") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!count($_POST["alertactions"])) {
			$errormessage = $_SWIFT["language"]["actioncnterror"];
		} else {
			insertAlertRule($_POST["title"], $_POST["alerttype"], $_POST["alertscope"], $_POST["departmentid"], $_POST["priorityid"], $_POST["ticketstatusid"], $_POST["execassign"], $_POST["alertactions"]);

			printRedirect(sprintf($_SWIFT["language"]["arinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=managealerts&insertrule=".urlencode($_POST["title"]));
			exit;
		}
	}

	renderTicketTree();
	$interface->staffHeader($_SWIFT["language"]["tickets"]." &gt; ".$_SWIFT["language"]["insertalert"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managealerts" title="'.$_SWIFT["language"]["managealerts"].'">'.$_SWIFT["language"]["managealerts"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertalert" title="'.$_SWIFT["language"]["insertalert"].'">'.$_SWIFT["language"]["insertalert"].'</a>', "", 2);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderAlertRuleForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managealerts");

	$interface->staffFooter();







/**
* ###############################################
* EDIT ALERT RULE
* ###############################################
*/
} else if ($eventaction == "editalert") {
	$_alertrule = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."alertrules` WHERE `alertruleid` = '". intval($_REQUEST["alertruleid"]) ."';");
	if (empty($_alertrule["alertruleid"]))
	{
		trigger_error($_SWIFT["language"]["invalidar"], E_USER_ERROR);
		exit;
	}

	$_alertactions = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."alertactions` WHERE `alertruleid` = '". intval($_alertrule["alertruleid"]) ."';");
	while ($dbCore->nextRecord())
	{
		$_alertactions[] = $dbCore->Record["actiontype"];
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!count($_POST["alertactions"])) {
			$errormessage = $_SWIFT["language"]["actioncnterror"];
		} else if ($_alertrule["alertscope"] == 1 && $_SWIFT["staff"]["tupdarule"] == "0") {
			$errormessage = $_SWIFT["language"]["nopermission"];
		} else {
			updateAlertRule($_alertrule["alertruleid"], $_POST["title"], $_POST["alerttype"], $_POST["alertscope"], $_POST["departmentid"], $_POST["priorityid"], $_POST["ticketstatusid"], $_POST["execassign"], $_POST["alertactions"]);

			printRedirect(sprintf($_SWIFT["language"]["areditconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=managealerts&editrule=".urlencode($_POST["title"]));
			exit;
		}
	}

	$template->assign("itemoptiontitle", $_SWIFT["language"]["alertoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["deleterule"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["ardelconfirmmsg"] .'\', \'index.php?_m=tickets&_a=managealerts&do=delete&alertruleid='. $_alertrule["alertruleid"] .'\')');

	$_POST = array_merge($_POST, $_alertrule);
	$_POST["alertactions"] = $_alertactions;

	renderTicketTree();
	$interface->staffHeader($_SWIFT["language"]["tickets"]." &gt; ".$_SWIFT["language"]["editalert"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managealerts" title="'.$_SWIFT["language"]["managealerts"].'">'.$_SWIFT["language"]["managealerts"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editalert&alertruleid='. intval($_alertrule["alertruleid"]) .'" title="'.$_SWIFT["language"]["editalert"].'">'.$_SWIFT["language"]["editalert"].'</a>', "", 2);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderAlertRuleForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managealerts");

	$interface->staffFooter();
}



?>